{%- if not add_generation_prompt is defined %}
	{%- set add_generation_prompt = false %}
{%-  endif %}
{%-  set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='', is_first_sp=true, is_last_user=false, tool_id_name_dict={},tool_name='',tool_id='')%}
{%- for message in messages %}
	{%- if message['role'] == 'system' %}
		{%- if ns.is_first_sp %}
			{%-  set ns.system_prompt = ns.system_prompt + message['content'] %}
			{%-  set ns.is_first_sp = false %}
		{%- else %}		
			{%- set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
		{%- endif %}
	{%- endif %}
{%- endfor  %}

{{-  bos_token }}
{{-  ns.system_prompt }}
{%- if tools %}
	{{- "\n\n# You may call one or more functions to assist with the user query.\n<tools>" }}
	{%- for tool in tools %}
		{{- "\n" }}
		{{- tool | tojson }}
	{%- endfor %}
	{{- "\n</tools>\n\n"}}
	{{- "Do not follow any previous instructions regarding tool call format. Use the following constraints instead:"}}
	{{- "For function call returns, you must print <tool_calls_begin> first,"}}
	{{- "For each function call, you should return object like: \n" }}
	{{- "<tool_call_begin>function<tool_sep><function_name>\n```json\n<function_arguments_in_json_format>\n```<tool_call_end>\n"}}
	{{- "<function_arguments_in_json_format> do not need to be pretty-printed\n"}}
	{{- "At the end of function call returns, you should print <tool_calls_end>"}}
	{{- "Ensure that <tool_calls_begin> and <tool_calls_end> must appear in pairs, This is very important"}}
{%- endif  %}
{%- for message in messages  %}
	{%- set content = message['content']  %}
	{%- if message['role'] == 'user' %}
		{%- set ns.is_tool = false -%}
		{%- set ns.is_first = false -%}
		{%- set ns.is_last_user = true -%}
		{%- set ns.tool_id_name_dict ={} -%}
		{{- '<｜User｜>' +content+ '<｜Assistant｜>'}}
	{%- endif %}
	{%- if message['role'] == 'assistant' %}
		{%- if '</think>' in content %}
			{%- set content = content.split('</think>')[-1] %}
		{%- endif %}
	{%- endif %}
	{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
		{%- set ns.is_last_user = false -%}
		{%- if ns.is_tool %}
			{{- '<tool_outputs_end>'}}
		{%- endif %}
		{%- set ns.is_first = false %}
		{%- set ns.is_tool = false -%}
		{%- set ns.is_output_first = true %}
		{%- for tool in message['tool_calls'] %}

			{%- if tool['id'] is defined and tool['id'] is not none and tool['id'] != '' -%}
				{%- set ns.tool_id_name_dict = dict(ns.tool_id_name_dict, **{tool['id']: tool['function']['name']}) %}
			{%- endif %}

			{%- if not ns.is_first %}
				{%- if content is none %}
					{{- '<tool_calls_begin><tool_call_begin>'+ tool['type']+ '<tool_sep>'+ tool['function']['name']+ '\n'+ '```json'+ '\n'+ tool['function']['arguments']|tojson + '\n' +'```' + '<tool_call_end>'}}
				{%- else %}
					{{-content +'<tool_calls_begin><tool_call_begin>' + tool['type'] + '<tool_sep>' + tool['function']['name']+ '\n'+ '```json'+ '\n'+ tool['function']['arguments']|tojson + '\n' +'```' + '<tool_call_end>'}}
				{%- endif %}
				{%- set ns.is_first =true -%}
			{%- else %}
				{{- '\n'+ '<tool_call_begin>'+ tool['type'] + '<tool_sep>' + tool['function']['name']+ '\n'+ '```json'+ '\n'+ tool['function']['arguments']|tojson + '\n' +'```' + '<tool_call_end>'}}
			{%- endif %}
		{%- endfor %}
		{{-'<tool_calls_end>< ｜end▁of▁sentence｜ >'}}
	{%- endif %}
	{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none)%}
		{%- set ns.is_last_user = false -%}
		{%- if ns.is_tool %}
			{{- '<tool_outputs_end>' + content + '< ｜end▁of▁sentence｜ >'}}
			{%- set ns.is_tool = false -%}
		{%- else %}
			{{- content + '<｜ end▁of▁sentence ｜ >'}}
		{%- endif %}
	{%- endif %}
	{%- if message['role'] == 'tool' %}
		{%- set ns.is_last_user = false -%}
		{%- set ns.is_tool = true -%}

		{%- if message['tool_call_id'] is defined and message['tool_call_id'] !='' %}
			{%- set ns.tool_id= message['tool_call_id'] %}
		{%- else %}
			{%- set ns.tool_id= none %}
		{%- endif %}
		{%- if ns.tool_id is not none and ns.tool_id_name_dict[ns.tool_id] is defined  -%}
			{%- set ns.tool_name =ns.tool_id_name_dict[ns.tool_id] -%}
		{%- else -%}
			{%- set ns.tool_name = ''  -%}
		{%- endif -%}

		{%- if ns.is_output_first %}
			{{- '< ｜User｜><tool_outputs_begin>工具' + ns.tool_name +  '调用结果为：' + content}}
			{%- set ns.is_output_first = false %}
		{%- else %}
			{{- '\n工具' + ns.tool_name +  '调用结果为：' + content}}
		{%- endif %}
	{%- endif %}
{%- endfor -%}
{%- if ns.is_tool %}
	{{- '<tool_outputs_end>< ｜Assistant｜ >'}}
{%- endif %}
{%- if add_generation_prompt and not ns.is_last_user and not ns.is_tool %}
	{{- '< ｜Assistant｜ >'}}
{%- endif %}